package com.zepher.tourism.controller.common;

import com.zepher.tourism.domain.Result;
import lombok.RequiredArgsConstructor;
import org.dromara.x.file.storage.core.FileInfo;
import org.dromara.x.file.storage.core.FileStorageService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * 通用Controller，处理文件上传等功能
 */
@Controller
@ResponseBody
@RequestMapping
@RequiredArgsConstructor
public class CommonController {

    private final FileStorageService fileStorageService;

    /**
     * 错误转发地址
     */
    @GetMapping("/common/error/{code}/{msg}")
    public Result error(@PathVariable("code") Integer code, @PathVariable("msg") String msg) {
        return Result.alert(code, msg);
    }

    /**
     * 上传图片 (原CommonController路径)
     */
    @PostMapping("/common/uploadImg")
    public Result uploadImg(@RequestParam("file") MultipartFile img) {
        return handleImageUpload(img);
    }

    /**
     * 上传图片 (原FileUploadController路径)
     */
    @PostMapping("/file/uploadImage")
    public Result uploadImage(@RequestParam("file") MultipartFile file) {
        return handleImageUpload(file);
    }

    /**
     * 处理图片上传的通用方法
     */
    private Result handleImageUpload(MultipartFile file) {
        if (file.isEmpty()) {
            return Result.fail("上传的图片不能为空!");
        }
        
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1).toLowerCase();
        if (!"jpeg".equals(suffix) && !"gif".equals(suffix) && !"png".equals(suffix) && !"bmp".equals(suffix) && !"jpg".equals(suffix)) {
            return Result.fail("请选择正确的图片格式");
        }

        return uploadToStorage(file);
    }

    /**
     * 上传视频 (原CommonController路径)
     */
    @PostMapping("/common/uploadVideo")
    public Result uploadVideoCommon(@RequestParam("file") MultipartFile file) {
        return handleVideoUpload(file);
    }

    /**
     * 上传视频 (原FileUploadController路径)
     */
    @PostMapping("/file/uploadVideo")
    public Result uploadVideo(@RequestParam("file") MultipartFile file) {
        return handleVideoUpload(file);
    }

    /**
     * 处理视频上传的通用方法
     */
    private Result handleVideoUpload(MultipartFile file) {
        if (file.isEmpty()) {
            return Result.fail("上传的视频不能为空!");
        }
        return uploadToStorage(file);
    }

    /**
     * 上传文件 (原CommonController路径)
     */
    @PostMapping("/common/uploadFile")
    public Result uploadFileCommon(@RequestParam("file") MultipartFile file) {
        return handleFileUpload(file);
    }

    /**
     * 上传文件 (原FileUploadController路径)
     */
    @PostMapping("/file/uploadFile")
    public Result uploadFile(@RequestParam("file") MultipartFile file) {
        return handleFileUpload(file);
    }

    /**
     * 处理文件上传的通用方法
     */
    private Result handleFileUpload(MultipartFile file) {
        if (file.isEmpty()) {
            return Result.fail("上传的文件不能为空!");
        }
        return uploadToStorage(file);
    }

    /**
     * 统一的文件存储处理方法
     */
    private Result uploadToStorage(MultipartFile file) {
        // 生成日期格式的路径
        String datePath = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
        
        try {
            FileInfo fileInfo = fileStorageService.of(file)
                    .setPath(datePath) // 设置日期格式的路径
                    .setPlatform("aliyun-oss-1") // 使用阿里云OSS存储
                    .upload();
            
            if (fileInfo != null) {
                return Result.success(fileInfo.getUrl());
            } else {
                return Result.fail("上传失败");
            }
        } catch (Exception e) {
            return Result.fail("上传失败: " + e.getMessage());
        }
    }
}
